package com.example.bbsbe.service;

import com.example.bbsbe.Repository.CommentsRepository;
import com.example.bbsbe.Repository.ProfileRepository;
import com.example.bbsbe.Repository.UserRepository;
import com.example.bbsbe.entity.comments;
import com.example.bbsbe.entity.profile;
import com.example.bbsbe.entity.user;
import jakarta.persistence.EntityManager;
import jakarta.persistence.PersistenceContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

@Service
@Transactional
public class CommentsService {
    private final CommentsRepository commentsRepository;
    private final ProfileRepository profileRepository;
    private final UserRepository userRepository;

    @PersistenceContext
    private EntityManager entityManager;
    public CommentsService(CommentsRepository commentsRepository, ProfileRepository profileRepository, UserRepository userRepository, EntityManager entityManager) {
        this.commentsRepository = commentsRepository;
        this.profileRepository = profileRepository;
        this.userRepository = userRepository;
        this.entityManager=entityManager;
    }
    public List<comments> findAll() {
        List<comments> comments=commentsRepository.findAll();

        for (comments entity : comments){
            entityManager.detach(entity);
            Long id=entity.getId();
            String profilename=entity.getProfilename();
            String owner=entity.getOwner();
            entity.setUrl("https://www.yushi.chat/api/comments/"+id+'/');
            entity.setProfilename("https://www.yushi.chat/api/profiles/"+profilename+'/');
            entity.setOwner("https://www.yushi.chat/api/users/"+owner+'/');
        }
        ;
        return comments;

    }
    public Map<String, Object> createComment(String profileId, String content, String username) {
        user owner = userRepository.findByName(username);

        Optional<profile> profile = profileRepository.findById(Long.valueOf(profileId));

        // 增加资料评论数
        profileRepository.incrementDiscuss(Long.valueOf(profileId), 1);

        // 增加用户经验值
        userRepository.incrementLevel(String.valueOf(owner.getId()), 2);

        comments comment = new comments();
        comment.setContent(content);
        comment.setOwner(String.valueOf(owner.getId()));
        comment.setProfilename(String.valueOf(profile.get().getId()));
        commentsRepository.save(comment);

        Map<String, Object> response = new HashMap<>();
        response.put("condition", 1);
        return response;
    }
    public List<Map<String, Object>> getUserComments(String username) {
        String userId = String.valueOf(userRepository.findIdByName(username).getId());

        List<comments> comments = commentsRepository.findByOwner(userId);
        List<Map<String, Object>> result = new ArrayList<>();

        for (comments comment : comments) {
            profile profile = profileRepository.getById(Long.valueOf(comment.getProfilename()));
            Map<String, Object> item = new HashMap<>();
            item.put("comid", comment.getId());
            item.put("content", comment.getContent());
            item.put("title", profile.getTitle());
            item.put("type", profile.getType());
            item.put("proid", profile.getId());
            result.add(item);
        }
        return result;
    }
    public Map<String, Object> deleteComment(Long commentId) {
        Optional<comments> comment = commentsRepository.findById(commentId);

        // 减少资料评论数
        profileRepository.incrementDiscuss(Long.valueOf(comment.get().getProfilename()), -1);

        commentsRepository.delete(comment.get());

        Map<String, Object> response = new HashMap<>();
        response.put("condition", 1);
        return response;
    }
}
